GetVoucherByCodeQueryHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 19
dl 0
loc 20
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A execute 0 13 2
1
import { QueryHandler } from '@nestjs/cqrs';
2
import { Inject } from '@nestjs/common';
3
import { IVoucherRepository } from 'src/Domain/School/Repository/IVoucherRepository';
4
import { VoucherNotFoundException } from 'src/Domain/School/Exception/VoucherNotFoundException';
5
import { GetVoucherByCodeQuery } from './GetVoucherByCodeQuery';
6
import { VoucherView } from '../../View/VoucherView';
7
8
@QueryHandler(GetVoucherByCodeQuery)
9
export class GetVoucherByCodeQueryHandler {
10
  constructor(
11
    @Inject('IVoucherRepository')
12
    private readonly voucherRepository: IVoucherRepository
13
  ) {}
14
15
  public async execute({ code }: GetVoucherByCodeQuery): Promise<VoucherView> {
16
    const voucher = await this.voucherRepository.findOneByCode(code);
17
18
    if (!voucher) {
19
      throw new VoucherNotFoundException();
20
    }
21
22
    return new VoucherView(
23
      voucher.getId(),
24
      voucher.getCode(),
25
      voucher.getEmail(),
26
      voucher.getSchool().getId(),
27
    );
28
  }
29
}
30